package day34.edu;

import org.junit.Test;

import java.io.*;

// Created: 05/10/2025, ChangJiang Ru
public class InputStreamReaderTest {
    @Test
    public void test1() throws IOException {
        // Create the file object
        File file = new File("Soccer_gbk.txt");

        // Create the File Input Stream object
        FileInputStream fis = new FileInputStream(file);

        // Create the Input Stream Reader object
//        InputStreamReader isr = new InputStreamReader(fis);
        InputStreamReader isr = new InputStreamReader(fis, "utf-8"); // translate the byte stream to char stream

        // Read input the data
        char[] cBuffered = new char[1024];
        int len;
        while((len = isr.read(cBuffered)) != -1)
        {
            String str = new String(cBuffered, 0, len);
            System.out.print(str);
        }

        fis.close();
    }
    
    @Test
    public void test2() throws IOException
    {
        File file1 = new File("Soccer_gbk.txt");
        File file2 = new File("Soccer_gbk_to_utf-8.txt");

        FileInputStream fis = new FileInputStream(file1);
        InputStreamReader isr = new InputStreamReader(fis, "gbk");

        FileOutputStream fos = new FileOutputStream(file2);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");

        char[] cBuffer = new char[1024];
        int len;
        while((len = isr.read(cBuffer)) != -1)
        {
            osw.write(cBuffer, 0, len);
        }

        System.out.println("Operator successfully");

        // Close source
        isr.close();
        osw.close();
    }
    @Test
    public void test3() throws IOException {
        File file = new File("object.txt");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));

        oos.writeUTF("Every day english, every day programming!!!");
        oos.flush();

        oos.writeObject("Control the life and the study");
        oos.flush();

        oos.close();
    }
    
    @Test
    public void test4() throws IOException, ClassNotFoundException {
        File file = new File("object.txt");

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));

        String str1 = ois.readUTF();
        System.out.println(str1);

        String str2 = (String) ois.readObject();
        System.out.println(str2);

        ois.close();
    }
    
    @Test
    public void test5() throws IOException {
        File file = new File("object1.bat");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));

        Person p1 = new Person("Tom", 15);
        oos.writeObject(p1);
        oos.flush();

        oos.close();
    }
    
    @Test
    public void test6() throws IOException, ClassNotFoundException {
        File file = new File("object1.bat");

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));

        Person person = (Person) ois.readObject();
        System.out.println(person);

        ois.close();
    }
    
    @Test
    public void test7() throws FileNotFoundException {
        PrintStream ps = new PrintStream("io.txt");

        ps.println("hello");
        ps.println(1);
        ps.println(1.5);

        System.setOut(ps);
        System.out.println("Every day study english and programming!!!");

        ps.close();
    }

    @Test
    public void test8()
    {
        for (int i = 0; i < 255; i++) {
            System.out.print((char) i);
            if(i % 50 == 0)
            {
                System.out.println();
            }
        }
    }
}

